<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Validate;

class Wxcode extends Base
{
    // 新增二维码名片
    public function wxcodeAdd()
    {
        $data = input();//token id managee_id wx_code_name wx_code max_number sort status

        if (empty($data['manage_id'])) apiReturn(201,'工号不能为空');
        if (empty($data['wx_code_name'])) apiReturn(201,'二维码名称不能为空');
        if (empty($data['wx_code'])) apiReturn(201,'二维码不能为空');
        if (!isset($data['max_number'])) apiReturn(201,'访问上线不能为空');
        if ($data['type'] == 2) {
            if (empty($data['source'])) apiReturn(201,'渠道不能为空');
            $media = db('crm_media')->where('id',$data['source'])->find();
            if (empty($media['desc'])) {
                apiReturn('渠道链接不能为空');
            }
            $data['card_link'] = $media['desc'];
            if ($media['desc'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login1') {
                $data['wx_path'] = 'pages/login/login1';
            }elseif ($media['desc'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login2') {
                $data['wx_path'] = 'pages/login/login2';
            }elseif ($media['desc'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login3') {
                $data['wx_path'] = 'pages/login/login3';
            }elseif ($media['desc'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login4') {
                $data['wx_path'] = 'pages/login/login4';
            }elseif ($media['desc'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login5') {
                $data['wx_path'] = 'pages/login/login5';
            }else{
                apiReturn(201,'链接错误');
            }
        }

        unset($data['token']);
        if (isset($data['id'])) {
            if (!empty($data['id'])) {
                db('crm_wxcode')->where('id',$data['id'])->update($data);
                apiReturn(200,'修改二维码名片成功');
            }else{
                apiReturn(201,'参数错误');
            }
        }else{
            $data['date'] = time();
            db('crm_wxcode')->insert($data);
            apiReturn(200,'新增二维码名片成功');
        }
    }

    // 二维码名片列表
    public function wxcodeList()
    {
        $data = input();
        $where = [];
        if (isset($data['wx_code_name'])) {
            if (!empty($data['wx_code_name'])) {
                $where['wx_code_name'] = ['like','%'.$data['wx_code_name'].'%'];
            }
        }
        if (isset($data['status'])) {
            if ($data['status'] !== '') {
                $where['status'] = $data['status'];
            }
        }
        if (isset($data['type'])) {
            if (!empty($data['type'])) {
                $where['type'] = $data['type'];
            }
        }
        if (isset($data['source'])) {
            if (!empty($data['source'])) {
                $where['source'] = $data['source'];
            }
        }
        $wxcodeList = db('crm_wxcode')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('date DESC')->select();
        foreach ($wxcodeList as $key => $value) {
            $wxcodeList[$key]['manage_name'] = db('crm_manage')->where('username',$value['manage_id'])->value('name');
            $wxcodeList[$key]['source'] = intval($value['source']);
            $wxcodeList[$key]['source_name'] = db('crm_media')->where('id',$value['source'])->value('media');
        }
        $count = db('crm_wxcode')->where($where)->count();

        $datas = array(
            'wxcodeList'=>$wxcodeList,
            'count'=>$count
        );

        apiReturn(200,'获取列表成功',$datas);
    }

    // 删除名片
    public function wxcodeDelete()
    {
        $data = input();//token id
        if (empty($data['id'])) {
            apiReturn(201,'id不能为空');
        }

        db('crm_wxcode')->where('id',$data['id'])->delete();
        apiReturn(200,'删除成功');
    }

    // 排序设置
    public function setSort()
    {
        $data = input();//token凭证 id sort排序

        $exist = db('crm_wxcode')->where('id',$data['id'])->find();
        if (empty($exist)) {
            apiReturn(201,'名片不存在');
        }else{
            if ($exist['sort'] == $data['sort']) {
                apiReturn(200,'修改排序成功');
            }else{
                db('crm_wxcode')->where('id',$data['id'])->update($data);
            }
        }
        apiReturn(200,'修改排序成功');
    }

    // 名片配置
    public function set()
    {
        $data = input();//token name微信描述 way(1轮询切换 2顺序切换 3随机切换) type(1累计限制 2每日限制) desc备注

        if (empty($data['name'])) apiReturn(201,'微信描述不能为空');
        if (empty($data['way'])) apiReturn(201,'二维码切换方式不能为空');
        if (empty($data['type'])) apiReturn(201,'二维码访问上限类型不能为空');

        $data['date'] = time();
        db('crm_wxcode_set')->where('id',1)->update($data);
        apiReturn(200,'操作成功');
    }

    // 获取名片配置
    public function setInfo()
    {
        $data = input();//token
        $info = db('crm_wxcode_set')->where('id',1)->find();
        apiReturn(200,'获取成功',$info);
    }

    // 链接列表
    public function linkList()
    {
        $datas = [
            ['id'=>1,'name'=>'https://api.xuexiang.com.cn/push/scheme/qr?type=login1'],
            ['id'=>2,'name'=>'https://api.xuexiang.com.cn/push/scheme/qr?type=login2'],
            ['id'=>3,'name'=>'https://api.xuexiang.com.cn/push/scheme/qr?type=login3'],
            ['id'=>4,'name'=>'https://api.xuexiang.com.cn/push/scheme/qr?type=login4'],
            ['id'=>5,'name'=>'https://api.xuexiang.com.cn/push/scheme/qr?type=login5']
        ];
        $links = db('crm_media')->column('desc');
        foreach ($links as $key => $value) {
            if ($value == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login1') {
                unset($datas[0]);
            }
            if ($value == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login2') {
                unset($datas[1]);
            }
            if ($value == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login3') {
                unset($datas[2]);
            }
            if ($value == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login4') {
                unset($datas[3]);
            }
            if ($value == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login5') {
                unset($datas[4]);
            }
        }
        $datas1 = [];
        foreach ($datas as $key => $value) {
            $datas1[] = $value;
        }
        apiReturn(200,'获取成功',$datas1);
    }

    // 新增渠道链接
    public function mediaLinkAdd()
    {
        $data = input();//token source link
        if (empty($data['source'])) apiReturn(201,'渠道不能为空');
        if (empty($data['link'])) apiReturn(201,'链接不能为空');

        $source = db('crm_media')->where('id',$data['source'])->find();
        $datas['desc'] = $data['link'];
        if ($data['link'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login1') {
            $datas['short_desc'] = 'https://dwz.cn/V0iYUwp7';
        }elseif ($data['link'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login2') {
            $datas['short_desc'] = 'https://dwz.cn/Cs8Is9YV';
        }elseif ($data['link'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login3') {
            $datas['short_desc'] = 'https://dwz.cn/0FDEY4yz';
        }elseif ($data['link'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login4') {
            $datas['short_desc'] = 'https://dwz.cn/HPNgBLvq';
        }elseif ($data['link'] == 'https://api.xuexiang.com.cn/push/scheme/qr?type=login5') {
            $datas['short_desc'] = 'https://dwz.cn/Cs8Is9YV';
        }else{
            apiReturn(201,'链接错误');
        }
        db('crm_media')->where('id',$data['source'])->update($datas);
        apiReturn(200,'操作成功');
    }
}
?>